#!/usr/bin/python
# -*- coding: utf-8 -*-
#-------------------------------------------------
#Create At: 2009-4-21 下午02:17:35 
#Create By: Fla.sam
#
#DESC: 
#
#-------------------------------------------------

from core_app.models import PureUser, Department
from django.contrib import admin
import unipath
from django.conf import settings
import os.path
import subprocess

def reset_home_dir( o ):
    '''
    #清理用户的主目录,本操作会删除用户的所有数据
    @param o:
    @type o:
    '''
    user_obj = o
    _h = unipath.Path( user_obj.homedir.encode( settings.FILE_SYSTEM_CODE ) )
    _homedir = _h.absolute()
        
    _public_dir = unipath.Path( _homedir, 'public' )
    _techer_dir = unipath.Path( _homedir, 'techers' )
    _student_dir = unipath.Path( _homedir, 'students' )          

    #remove all link
    try:
        _public_dir.remove()
        _techer_dir.remove()
        _student_dir.remove()
    except:
        pass
                
    _homedir.rmtree( parents = False )
    _homedir.mkdir( parents = True )
    
    #make a public link for all user
    _public_dir.make_relative_link_to( os.path.join( settings.PURE_FTPD_ROOT_PATH, 'public' ) )
    
    if user_obj.techer:
        subprocess.Popen('chown %s:%s -R %s' % (settings.PURE_TECHER_UID, settings.PURE_DEFAULT_GID,str( _homedir )), shell=True)
        subprocess.Popen( 'setfacl -m user:%s:rwx -R %s' % ( settings.PURE_ADMIN_UID, str( _homedir ) ), shell = True )
    else:
        subprocess.Popen('chown %s:%s -R %s' % (settings.PURE_STUDENT_UID, settings.PURE_DEFAULT_GID,str( _homedir )), shell=True)
        subprocess.Popen( 'setfacl -m user:%s:rwx -R %s' % ( settings.PURE_ADMIN_UID, str( _homedir ) ), shell = True )
        # make a link for all student
        _techer_dir.make_relative_link_to( os.path.join( settings.PURE_FTPD_ROOT_PATH, 'techers' ) )

def make_homedir( o ):
    '''
    #建立用户的主目录结构
    @param o:
    @type o:
    '''
    user_obj = o
    _h = unipath.Path( user_obj.homedir.encode( settings.FILE_SYSTEM_CODE ) )
    _homedir = _h.absolute()
        
    _public_dir = unipath.Path( _homedir, 'public' )
    _techer_dir = unipath.Path( _homedir, 'techers' )
    _student_dir = unipath.Path( _homedir, 'students' )          
        
    #_home_dir.rmtree( parents = False )
    _homedir.mkdir( parents = True )
    subprocess.Popen( 'chmod 1755 -R %s' % str( _homedir ) , shell = True )
    #remove all link
    try:
        _public_dir.remove()
        _techer_dir.remove()
        _student_dir.remove()
    except:
        pass    
    
    #make a public link for all user
    _public_dir.make_relative_link_to( os.path.join( settings.PURE_FTPD_ROOT_PATH, 'public' ) )
    
    if user_obj.techer:
        subprocess.Popen('chown %s:%s -R %s' % (settings.PURE_TECHER_UID, settings.PURE_DEFAULT_GID,str( _homedir )), shell=True)
        subprocess.Popen( 'setfacl -m user:%s:rwx -R %s' % ( settings.PURE_ADMIN_UID, str( _homedir ) ), shell = True )
        
        #建立允许学生上传的目录
        
    else:
        subprocess.Popen('chown %s:%s -R %s' % (settings.PURE_STUDENT_UID, settings.PURE_DEFAULT_GID,str( _homedir )), shell=True)
        subprocess.Popen( 'setfacl -m user:%s:rwx -R %s' % ( settings.PURE_ADMIN_UID, str( _homedir ) ), shell = True )
        # make a link for all student
        _techer_dir.make_relative_link_to( os.path.join( settings.PURE_FTPD_ROOT_PATH, 'techers' ) )


def mark_enabled( modeladmin, request, queryset ):
    queryset.update( useable = True )
    for user_obj in queryset:
        make_homedir(user_obj)
mark_enabled.short_description = u'标记用户可用'

def mark_disable( modeladmin, request, queryset ):
    queryset.update( useable = False )
mark_disable.short_description = u'标记用户禁止'

def clean_user_directory( modeladmin, request, queryset ):
    for user_obj in queryset:
        reset_home_dir(user_obj)
clean_user_directory.short_description = u'清理用户目录下的所有文件,并重建基本目录'

class PureUserAdmin( admin.ModelAdmin ):

    actions_on_top = True
    actions_on_bottom = True
    list_display = ( 'loginname', \
                    'name', \
                    'useable', \
                    'techer',
                    'homedir', \
                    'department', \
                    'gender', \
                    'quotafiles', \
                    'quotasize', \
                    'bandwidthup', \
                    'bandwidthdn', \
                    'createdatetime' )
    
    fieldsets = ( 
                  ( u'用户状态:', {'fields':( 'useable', 'techer', 'loginname', 'password' )} ),
                  ( u'用户信息:', {'fields':( 'name', 'department', 'gender' )} ),
                  ( u'FTP限额:', {'fields':( 'homedir', 'quotafiles', 'quotasize', 'bandwidthup', 'bandwidthdn' )} )
                   )
    
    list_filter = ( 'useable', 'techer' )
    search_fields = ['loginname', 'name']
    actions = [mark_enabled, mark_disable, clean_user_directory ]

class DepartmentAdmin( admin.ModelAdmin ):
    list_display = ( 'name', 'e_name' )

admin.site.register( PureUser, PureUserAdmin )
admin.site.register( Department, DepartmentAdmin )
